/**
 * 
 */
package tk.redbluelotto.service;

import java.util.HashMap;
import java.util.Map;

import tk.redbluelotto.Constants;
import tk.redbluelotto.model.RedBall;
import tk.redbluelotto.util.CalculateUtil;

/**
 * 与运算规则--对目标数做求与运算
 * MOD_3=1:2:3,MOD_2=3:3,PART_3=1:2:3,MOD_10=?:?:?:?:?:?:?:2:?:?,PRIME=1
 * 不确定以?符合替代。
 * 
 * @author zb
 * 
 */
public class AndRule implements IRule {

	private Map<String, Integer[]> ruleMap = new HashMap<String, Integer[]>(2);

	public AndRule(String ruleExpress) {
		init(ruleExpress);
	}

	@Override
	public void init(String ruleExpress) {
		String[] rules = ruleExpress.split(",");
		for (String rule : rules) {
			String[] operData = rule.split("=");
			String[] dataStrings = operData[1].split(":");
			ruleMap.put(operData[0], CalculateUtil.str2Int(dataStrings));
		}
	}

	@Override
	public boolean isSatisfied(RedBall number) {
		for (String oper : ruleMap.keySet()) {
			for (int i = 0; i < ruleMap.get(oper).length; i++) {
				if (ruleMap.get(oper)[i] != null && Long.bitCount(Constants.getRULE().get(oper)[i] & number.idx) != ruleMap.get(oper)[i]) {
					return false;
				}
			}
		}
		return true;
	}

}
